// Learn cc.Class:
//  - https://docs.cocos.com/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

const block_State = {
    idle: 0,
    grow: 1,
    stop: 2
}

cc.Class({
    extends: cc.Component,

    properties: {
        blockNode: cc.Node,
        baseNode: [cc.Node],
        wallNode: [cc.Node],
        scoreLabel: cc.Label

    },


    onLoad() {
        this.node.on('touchstart', this.grow, this);
        this.node.on('touchend', this.stop, this);

        this.init()

    },



    start () {

    },

    // update (dt) {},

    /**
     *  初始化
     */
    init() {
        this.score = 0
        this.nextLevel()
    },

    placeWall(node, desX) {
        node.runAction(cc.moveTo(0.5, cc.v2(desX, node.y)).easing(cc.easeQuarticActionIn()))
    },

    // 重构墙体
    resetWall() {
        let baseGap = 100 + Math.random() * 100
        let wallGap = baseGap + Math.random() * 80 + 30
        
        this.placeWall(this.baseNode[0], -baseGap/2)
        this.placeWall(this.baseNode[1], baseGap/2)
        this.placeWall(this.wallNode[0], -wallGap/2)
        this.placeWall(this.wallNode[1], wallGap/2)
        
    },

    // 重置白块
    resetBlock() {
        this.blockNode.runAction(cc.sequence(
            cc.spawn(
                cc.rotateTo(0.5, -45),
                cc.moveTo(0.5, cc.v2(0, 450)),
                cc.scaleTo(0.5, 1)
            ),
            cc.callFunc(() => {

            })
        ))

        this.updateColor(this.blockNode)

    },

    updateColor(node) {
        let colors = ["#33B5E5","#0099CC","#AA66CC","#9933CC","#99CC00","#669900","#FFBB33","#FF8800","#FF4444","#CC0000"]
        node.color = cc.Color.BLACK.fromHEX(colors[parseInt(Math.random() * colors.length)])
    },

    // 更新颜色
    updateBgColor() {
        this.updateColor(this.node)
    },

    // 重置场景
    nextLevel() {
        this.state = block_State.idle
        this.resetWall()
        this.resetBlock()

    },

    // 更新分数
    updateScore(num) {
        this.score += num
        this.scoreLabel.string = this.score
    },



    grow() {
        if(this.state != block_State.idle) {
            return
        }
        this.state = block_State.grow

        let seq = cc.sequence(
            cc.scaleTo(1, 4),
            cc.callFunc(() => {

            })
        )

        this.growActoion = this.blockNode.runAction(seq);
    },

    stop() {
        if(this.state != block_State.grow) {
            return
        }
        this.state = block_State.stop

        this.blockNode.stopAction(this.growActoion);

        this.blockNode.runAction(cc.sequence(
            cc.rotateTo(0.15, 0),
            cc.callFunc(() => {
                if(this.blockNode.width * this.blockNode.scaleX <= this.baseNode[1].x - this.baseNode[0].x) {
                    this.blockNode.runAction(cc.sequence(
                        cc.moveTo(0.7, cc.v2(0, -1000)),
                        cc.callFunc(() => {
                            this.gameOver()
                        })
                    ))
                }else{
                    if(this.blockNode.width * this.blockNode.scaleX <= this.wallNode[1].x - this.wallNode[0].x) {
                        this.bouce(true)
                        // 得1分
                        this.updateScore(1)

                    }else{
                        this.bouce(false)
                        // 变色
                        this.updateBgColor()

                    }
                    
                }

            })
        ))
    },

    bouce(success) {
        let desY = -(cc.winSize.height / 2 - this.baseNode[0].height - this.blockNode.height*this.blockNode.scaleX/2)
        if(!success) {
            desY += this.wallNode[0].height
        }

        this.blockNode.runAction(cc.sequence(
            cc.moveTo(0.7, cc.v2(0, desY)).easing(cc.easeBackInOut()),
            cc.callFunc(() => {
                // 刷新场景
                this.nextLevel()
            })
        ))
    },

    gameOver() {
        cc.director.loadScene('game')
    }
});
